#-*- coding: utf-8 -*-

import os

from PyQt5.QtCore import QAbstractListModel, QModelIndex, QVariant, Qt, pyqtSlot, QUrl
from PyQt5.QtGui import QDesktopServices

class MyModel(QAbstractListModel):
    def __init__(self, parent=None):
        super(MyModel, self).__init__(parent)
 
    def rowCount(self, parent=QModelIndex()):
        return len(self._data)
 
    def data(self, index, role=Qt.DisplayRole):
        if 0 <= index.row() < self.rowCount():
            if role == (Qt.UserRole + 1):
                return QVariant(self.getVideoFile(index.row()))
            elif role == (Qt.UserRole + 2):
                return QVariant(self.getPictureFile(index.row()))
            elif role == (Qt.UserRole + 3):
                return QVariant(self.getFileName(index.row()))

        return QVariant()
    
    def roleNames(self):
        roles = {
            Qt.UserRole + 1: b'picture',
        }
        return roles
    
    def getVideoFile(self, index):
        return self._data[index][0]
    
    def getPictureFile(self, index):
        file:str = "file:\\%s" % self._data[index][1]
        return file
    
    def getFileName(self, index):
        return self._data[index][2]
    
    @pyqtSlot()
    def nextPage(self):            
        self.beginResetModel()
        self.currentPageIndex = self.currentPageIndex + 1
        self._data = self.getPageData(self.currentPageIndex, self.dataCount)
        
        topLeft = self.createIndex(0,0)
        bottomRight = self.createIndex(self.rowCount(), 0)
        self.dataChanged.emit(topLeft, bottomRight )
            
    
    @pyqtSlot()
    def previousPage(self):
        if self.currentPageIndex > 0:            
            self.currentPageIndex = self.currentPageIndex - 1
            self._data = self.getPageData(self.currentPageIndex, self.dataCount)
            
            topLeft = self.createIndex(0,0)
            bottomRight = self.createIndex(self.rowCount(), 0)
            self.dataChanged.emit(topLeft, bottomRight )
            
    @pyqtSlot(str)
    def play(self, video):
        file = os.path.abspath(video)
        file:str = "file:///%s" % file
        
        url = QUrl(file, QUrl.TolerantMode)
        if QDesktopServices.openUrl(url):
            print('Video player started successfully.')
            
            data = self.database.select_video(video, 播放次数 = True)[0]
            data = data + 1
            self.database.update_video(video, 播放次数 = data)
        else:
            print("Failed to start video player.")
    
    def getPageData(self, page_number, page_size):
        offset = (page_number - 1) * page_size
        count = len(self.datas)
        
        datas = []
        if offset >=0 and offset < count:
            if offset + page_size >= count:
                page_size = count - offset
                
            for i in range(page_size):
                video = self.datas[offset + i]
                data = (video[0], video[2], video[1])
                datas.append(data)
        
        return datas